package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.BaseTrademark;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.atguigu.gmall.product.vo.BaseTrademarkVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lfy
 * @Description
 * @create 2023-07-12 9:09
 */
@RequestMapping("/admin/product")
@RestController
public class TrademarkRestController {

    @Autowired
    BaseTrademarkService baseTrademarkService;



    /**
     * 分页查询品牌列表
     * @param pn
     * @param ps
     * @return
     */
    @GetMapping("/baseTrademark/{pn}/{ps}")
    public Result baseTrademark(@PathVariable("pn") Long pn,
                                @PathVariable("ps") Long ps){

        //接受分页条件
        Page<BaseTrademark> page = new Page<>(pn,ps);

        //分页查询，封装分页结果
        Page<BaseTrademark> result = baseTrademarkService
                .lambdaQuery()
                .page(page);


        return Result.ok(result);
    }


    /**
     * 品牌保存
     * @param vo
     * @return
     */
    @PostMapping("/baseTrademark/save")
    public Result baseTrademarkSave(@RequestBody BaseTrademarkVo vo){


        BaseTrademark entity = new BaseTrademark();
        BeanUtils.copyProperties(vo,entity); //属性拷贝

        //保存品牌
        baseTrademarkService.save(entity);

        return Result.ok();
    }

    /**
     * 查询某个品牌完整信息
     * @param id
     * @return
     */
    @GetMapping("/baseTrademark/get/{id}")
    public Result trademarkGet(@PathVariable("id") Long id){

        BaseTrademark trademark = baseTrademarkService.getById(id);

        return Result.ok(trademark);
    }

    /**
     * 修改品牌
     * @param vo
     * @return
     */
    @PutMapping("/baseTrademark/update")
    public Result update(@RequestBody  BaseTrademarkVo vo){


        //1、接受前端数据
        BaseTrademark entity = new BaseTrademark();
        BeanUtils.copyProperties(vo,entity);

        //2、修改品牌
        baseTrademarkService.lambdaUpdate()
                .eq(BaseTrademark::getId,entity.getId())
                .update(entity);


        return Result.ok();
    }

    @DeleteMapping("/baseTrademark/remove/{id}")
    public Result remove(@PathVariable("id") Long id){

        baseTrademarkService.removeById(id);

        return Result.ok();
    }


    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList(){

        List<BaseTrademark> list = baseTrademarkService.list();

        return Result.ok(list);
    }
}
